package com.angis.fx.activity.jcdj;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.angis.fx.activity.R;
import com.angis.fx.adapter.MultipleAdapter2;
import com.angis.fx.db.WGTypeDBHelper;

public class MultipleDialogActivity extends Activity {
	
	private MultipleAdapter2 mMultipleDialogListAdapter2;
//	private Set<Integer> mPos;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LayoutInflater lLayoutInflater = LayoutInflater.from(MultipleDialogActivity.this);
		View lView = lLayoutInflater.inflate(R.layout.multiple_dialog, null);
		ListView mListView = (ListView)lView.findViewById(R.id.multiple_dialoglist);
//		mPos = new HashSet<Integer>();
		if (mMultipleDialogListAdapter2 ==null){
			mMultipleDialogListAdapter2 = new MultipleAdapter2(MultipleDialogActivity.this);
			List<String> lDataList = new ArrayList<String>();
			try {
				String lType = getIntent().getExtras().getString("type");
				WGTypeDBHelper lDBHelper = new WGTypeDBHelper(MultipleDialogActivity.this);
				Cursor lCursor = lDBHelper.getWGTypeByConditionType(lType);
				if(lCursor.moveToFirst()) {
					do {
						String type = lCursor.getString(2);
						lDataList.add(type);
					} while (lCursor.moveToNext());
				}
				lCursor.close();
				lDBHelper.closeDB();
			} catch (Exception e) {
				//
			}
			mMultipleDialogListAdapter2.setData(lDataList);
			mListView.setAdapter(mMultipleDialogListAdapter2);
			mListView.setItemsCanFocus(true);
		}
		
		setContentView(lView);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, "确定");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			StringBuffer lSb = new StringBuffer();
			int lResult = getIntent().getExtras() == null ? 1 : getIntent().getExtras().getInt("RESULTCODE");
			for (Integer pos : mMultipleDialogListAdapter2.getPosition()) {
				com.angis.fx.adapter.MultipleAdapter2.ViewHolder lHolder = mMultipleDialogListAdapter2.getHolder(pos);
				lSb.append(lHolder.getName().getText().toString()).append("#");
				if (lHolder.getmCount().getText()!=null)
					lSb.append(lHolder.getmCount().getText().toString()).append("#");
				else 
					lSb.append(" ").append("#");
				if (lHolder.getmRemark()!=null)
					lSb.append(lHolder.getmRemark().getText().toString()).append(";");
				else 
					lSb.append(" ").append(";");
			}
			Intent lIntent = new Intent();
			Bundle lBundle = new Bundle();
			lBundle.putString("JCJG", lSb.toString());
			lIntent.putExtras(lBundle);
			MultipleDialogActivity.this.setResult(lResult, lIntent);
			MultipleDialogActivity.this.finish();
		}
		return super.onOptionsItemSelected(item);
	}

	
}
